package com.primecodegen.common.AttributesProperties;

/**
 *----------------------------------------------------------
 *----------------------------------------------------------
 * @author Mohammad Awwad
 * Version: v0.0
 * contact: mhmdawwad@gmail.com
 *-----------------------------------------------------------
 *-----------------------------------------------------------
 */
import java.sql.*;
import java.util.*;
import com.primecodegen.common.base.*;
import com.primecodegen.util.*;
import java.util.logging.*;

public class AttributesPropertiesDAO extends BaseDAO {

    protected static String TblName = "Attributes_Properties";
    protected java.sql.Connection attributesPropertiesConn;
    protected static final Logger logger = Logger.getLogger(AttributesPropertiesDAO.class.getName());

    public AttributesPropertiesDAO() {
        super();
    }

    public int insert(IBaseTable attributesPropertiesTable) throws Exception {


        final boolean isConnSupplied = (attributesPropertiesConn != null);
        AttributesPropertiesTable newRecord = (AttributesPropertiesTable) attributesPropertiesTable;
        int insertValue = ComConstants.UNREALISTIC_INT;
        Connection con = null;
        PreparedStatement PrepSt = null;
        String sInsertSQL = null;
        StringBuffer sbSQL = new StringBuffer();

        if (attributesPropertiesTable == null) {
            throw new Exception("DAOException in AttributesPropertiesDAO class :attributesPropertiesTable is Null");
        }
        sInsertSQL = "INSERT INTO " + AttributesPropertiesDAO.TblName + " (";
        try {
            con = isConnSupplied ? attributesPropertiesConn : ResourceManager.getConnection();

            String sComma = "";
            int iColCount = 0;


            if (!util.isEmpty(newRecord.getID())) {
                iColCount = iColCount + 1;
                sbSQL.append(sComma).append(AttributesPropertiesDBCols.ID);
                sComma = ",";
            }

            if (!util.isEmpty(newRecord.getMainPrjID())) {
                iColCount = iColCount + 1;
                sbSQL.append(sComma).append(AttributesPropertiesDBCols.MAINPRJID);
                sComma = ",";
            }

            if (!util.isEmpty(newRecord.getCode())) {
                iColCount = iColCount + 1;
                sbSQL.append(sComma).append(AttributesPropertiesDBCols.CODE);
                sComma = ",";
            }

            if (!util.isEmpty(newRecord.getType())) {
                iColCount = iColCount + 1;
                sbSQL.append(sComma).append(AttributesPropertiesDBCols.TYPE);
                sComma = ",";
            }

            if (!util.isEmpty(newRecord.getDescr())) {
                iColCount = iColCount + 1;
                sbSQL.append(sComma).append(AttributesPropertiesDBCols.DESCR);
                sComma = ",";
            }


            sbSQL.append(") VALUES (");
            sComma = "";
            for (int i = 0; i < iColCount; i++) {
                sbSQL.append(sComma).append("?");
                sComma = ",";
            }
            sbSQL.append(");");

            sInsertSQL = sInsertSQL + sbSQL.toString();

            PrepSt = con.prepareStatement(sInsertSQL,
                    Statement.RETURN_GENERATED_KEYS);
            int i = 1;



            if (!util.isEmpty(newRecord.getID())) {
                PrepSt.setInt(i++, newRecord.getID());
            }

            if (!util.isEmpty(newRecord.getMainPrjID())) {
                PrepSt.setInt(i++, newRecord.getMainPrjID());
            }

            if (!util.isEmpty(newRecord.getCode().trim())) {
                PrepSt.setString(i++, newRecord.getCode().trim());
            }

            if (!util.isEmpty(newRecord.getType().trim())) {
                PrepSt.setString(i++, newRecord.getType().trim());
            }

            if (!util.isEmpty(newRecord.getDescr().trim())) {
                PrepSt.setString(i++, newRecord.getDescr().trim());
            }


            insertValue = PrepSt.executeUpdate();
            ResultSet rsIDJustInserted = PrepSt.getGeneratedKeys();
            if (rsIDJustInserted.next()) {
                insertValue = rsIDJustInserted.getInt(1);
            }



        } catch (SQLException sqlEx) {
            logger.log(Level.WARNING, "SQLException in AttributesPropertiesDAO.insert method Message: {0}", sqlEx.getMessage());
            throw new Exception("SQLException in AttributesPropertiesDAO.insert method");
        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception in AttributesPropertiesDAO.insert method Message: {0}", e.getMessage());
            throw new Exception("Exception in AttributesPropertiesDAO.insert method");
        } finally {
            ResourceManager.close(PrepSt);
            if (!isConnSupplied) {
                ResourceManager.close(con);
            }
        }

        return insertValue;
    }

    public int deleteByID(int IDPar) throws Exception {

        final boolean isConnSupplied = (attributesPropertiesConn != null);
        int ID = (int) IDPar;
        Connection con = null;
        PreparedStatement stmnt = null;
        int noOfRowsDeleted = ComConstants.UNREALISTIC_INT;
        String deleteSQL = "DELETE FROM " + AttributesPropertiesDAO.TblName + " WHERE " + AttributesPropertiesDBCols.ID + " = ?";
        try {
            con = isConnSupplied ? attributesPropertiesConn : ResourceManager.getConnection();
            stmnt = con.prepareStatement(deleteSQL);
            stmnt.setInt(1, ID);

            noOfRowsDeleted = stmnt.executeUpdate();



        } catch (SQLException sqlEx) {
            logger.log(Level.WARNING, "SQLException in AttributesPropertiesDAO.DeleteByID method Message: {0}", sqlEx.getMessage());
            throw new Exception("SQLException in AttributesPropertiesDAO.deleteByID method"
                    + sqlEx.getMessage());
        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception in AttributesPropertiesDAO.DeleteByID method Message: {0}", e.getMessage());
            throw new Exception("Exception in AttributesPropertiesDAO.deleteByID method"
                    + e.getMessage());
        } finally {
            if (!isConnSupplied) {
                ResourceManager.close(con);
            }
        }
        return noOfRowsDeleted;
    }

    public int updateByCriterion(IBaseTable attributesPropertiesSrchCriterionDTOPar,
            IBaseTable updatedRecordPar) throws Exception {

        final boolean isConnSupplied = (attributesPropertiesConn != null);
        AttributesPropertiesTable attributesPropertiesSrchCriterionDTO = (AttributesPropertiesTable) attributesPropertiesSrchCriterionDTOPar;
        AttributesPropertiesTable updatedRecord = (AttributesPropertiesTable) updatedRecordPar;
        int iNoOfRecordsUpdated = -1;
        Connection con = null;
        PreparedStatement stmnt = null;
        try {

            con = isConnSupplied ? attributesPropertiesConn : ResourceManager.getConnection();
            StringBuffer sbUpdateSQLStmnt = new StringBuffer();
            String sComma = "";
            sbUpdateSQLStmnt.append("UPDATE ").append(AttributesPropertiesDAO.TblName).append(" SET ");



            if (!util.isEmpty(updatedRecord.getID())) {
                sbUpdateSQLStmnt.append(sComma).append(AttributesPropertiesDBCols.ID + " = ").append("?");
                sComma = ",";
            }

            if (!util.isEmpty(updatedRecord.getMainPrjID())) {
                sbUpdateSQLStmnt.append(sComma).append(AttributesPropertiesDBCols.MAINPRJID + " = ").append("?");
                sComma = ",";
            }

            if (!util.isEmpty(updatedRecord.getCode())) {
                sbUpdateSQLStmnt.append(sComma).append(AttributesPropertiesDBCols.CODE + " = ").append("?");
                sComma = ",";
            }

            if (!util.isEmpty(updatedRecord.getType())) {
                sbUpdateSQLStmnt.append(sComma).append(AttributesPropertiesDBCols.TYPE + " = ").append("?");
                sComma = ",";
            }

            if (!util.isEmpty(updatedRecord.getDescr())) {
                sbUpdateSQLStmnt.append(sComma).append(AttributesPropertiesDBCols.DESCR + " = ").append("?");
                sComma = ",";
            }


            sbUpdateSQLStmnt.append(getUpdateWhereClause(attributesPropertiesSrchCriterionDTO));

            stmnt = con.prepareStatement(sbUpdateSQLStmnt.toString());
            int i = 1;

            if (!util.isEmpty(updatedRecord.getID())) {
                stmnt.setInt(i++, updatedRecord.getID());
            }

            if (!util.isEmpty(updatedRecord.getMainPrjID())) {
                stmnt.setInt(i++, updatedRecord.getMainPrjID());
            }

            if (!util.isEmpty(updatedRecord.getCode())) {
                stmnt.setString(i++, updatedRecord.getCode());
            }

            if (!util.isEmpty(updatedRecord.getType())) {
                stmnt.setString(i++, updatedRecord.getType());
            }

            if (!util.isEmpty(updatedRecord.getDescr())) {
                stmnt.setString(i++, updatedRecord.getDescr());
            }



            iNoOfRecordsUpdated = stmnt.executeUpdate();

        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception in AttributesPropertiesDAO.updateByCriterion method Message: {0}", e.getMessage());
            throw new Exception(
                    "Exception in AttributesPropertiesDAO.updateByCriterion method ");
        } finally {
            if (!isConnSupplied) {
                ResourceManager.close(con);
            }
        }

        return iNoOfRecordsUpdated;
    }

    public int updateByID(IBaseTable newRecordPar) throws Exception {
        AttributesPropertiesTable newRecord = (AttributesPropertiesTable) newRecordPar;
        if (newRecord != null) {
            AttributesPropertiesTable contentsCategorySrchCriterionDTO = new AttributesPropertiesTable();
            contentsCategorySrchCriterionDTO.setID(newRecord.getID());
            return this.updateByCriterion(contentsCategorySrchCriterionDTO, newRecord);
        } else {
            logger.log(Level.WARNING, "Exception in method AttributesPropertiesDAO.update.newRecord passed is null.");
            throw new Exception(
                    "Exception in method AttributesPropertiesDAO.update.newRecord passed is null.");
        }
    }

    private String getUpdateWhereClause(AttributesPropertiesTable attributesPropertiesSrchCriterionDTO) {
        String sWhere = "";
        if (attributesPropertiesSrchCriterionDTO.getID() != ComConstants.UNREALISTIC_INT) {
            sWhere = " WHERE ID = " + attributesPropertiesSrchCriterionDTO.getID();
        }
        return sWhere;
    }

    public IBaseTable getByID(int ID) throws Exception {

        if (!util.isEmpty(ID)) {
            AttributesPropertiesTable attributesPropertiesSrchCriterionDTO = new AttributesPropertiesTable();
            attributesPropertiesSrchCriterionDTO.setID(ID);
            attributesPropertiesSrchCriterionDTO.setFilter(true);
            ArrayList<AttributesPropertiesTable> attributesPropertiesTables = this.getByCriterionOrderedBy(
                    attributesPropertiesSrchCriterionDTO, null);
            AttributesPropertiesTable attributesPropertiesTable = null;
            if (attributesPropertiesTables != null) {
                Iterator<AttributesPropertiesTable> it = attributesPropertiesTables.iterator();
                while (it.hasNext()) {
                    attributesPropertiesTable = (AttributesPropertiesTable) it.next();
                }
            }

            return attributesPropertiesTable;
        } else {
            logger.log(Level.WARNING, "Exception in method AttributesPropertiesDAO.getByID.ID passed is unrealistic.");
            throw new Exception(
                    "Exception in method AttributesPropertiesDAO.getByID.ID passed is unrealistic.");
        }
    }

    public boolean deleteMainProjectIsSafe(int mainPrjID) throws Exception {
        ArrayList<AttributesPropertiesTable> mainPrjTables = new ArrayList<AttributesPropertiesTable>();
        if (!util.isEmpty(mainPrjID)) {
            AttributesPropertiesTable attributesPropertiesSrchCriterionDTO = new AttributesPropertiesTable();
            attributesPropertiesSrchCriterionDTO.setMainPrjID(mainPrjID);
            attributesPropertiesSrchCriterionDTO.setFilter(true);
            mainPrjTables = this.getByCriterionOrderedBy(attributesPropertiesSrchCriterionDTO, null);

            if (mainPrjTables.size() > 0) {
                return false;//Delete is SAFE, Group Empty
            } else {
                return true;
            }

        } else {
            logger.log(Level.WARNING, "Exception in method AttributesPropertiesDAO.getByID.ID passed is unrealistic.");
            throw new Exception(
                    "Exception in method AttributesPropertiesDAO.getByID.ID passed is unrealistic.");
        }
    }

    public ArrayList<AttributesPropertiesTable> getByCriterionOrderedBy(
            IBaseTable attributesPropertiesSrchCriterionDTOPar, ColToOrderBy colToOrderBy)
            throws Exception {


        final boolean isConnSupplied = (attributesPropertiesConn != null);
        AttributesPropertiesTable attributesPropertiesSrchCriterionDTO = (AttributesPropertiesTable) attributesPropertiesSrchCriterionDTOPar;
        Connection con = null;
        PreparedStatement stmnt = null;
        ResultSet rs = null;

        String sWhereClause = "";

        String sOrderBy = "";
        if (colToOrderBy != null && colToOrderBy.Name != null) {
            sOrderBy = this.getOrderBy(colToOrderBy);
        }
        if (attributesPropertiesSrchCriterionDTO != null && attributesPropertiesSrchCriterionDTO.isFilter()) {
            try {
                sWhereClause = this.getRetrieveWhereClause(attributesPropertiesSrchCriterionDTO);
            } catch (Exception e) {
                System.out.println("getByCriterionOrderedBy.Exception..");
                throw new Exception(" AttributesProperties.getByCriterionOrderedBy.Exception:  " + e.getMessage());
            }

            if (sWhereClause != null && !sWhereClause.trim().equalsIgnoreCase("")) {
                sWhereClause = " WHERE " + sWhereClause;
            }
        }
        ArrayList<AttributesPropertiesTable> attributesPropertiesTables = new ArrayList<AttributesPropertiesTable>();

        try {
            con = isConnSupplied ? attributesPropertiesConn : ResourceManager.getConnection();

           // String sSelectSQL = "SELECT `attributes_properties`.`ID`,`attributes_properties`.`MainPrjID`,`attributes_properties`.`Code`, `attributes_properties`.`Type`,`attributes_properties`.`Descr`,`main_project`.`ID`, `main_project`.`Name` FROM `new_src_code_gen`.`attributes_properties`inner join `new_src_code_gen`.`main_project` on `attributes_properties`.`MainPrjID` = `main_project`.`ID`;";
           StringBuilder sSelectSQL = new StringBuilder();
           sSelectSQL
                   .append("SELECT ")
                   .append("  attributes_properties.ID ").append(" AS ").append(AttributesPropertiesDBCols.ID)
                   .append(", attributes_properties.MainPrjID ").append(" AS ").append(AttributesPropertiesDBCols.MAINPRJID)
                   .append(", main_project.Name ").append(" AS ").append(AttributesPropertiesDBCols.MAINPRJNAME)
                   .append(", attributes_properties.Code ").append(" AS ").append(AttributesPropertiesDBCols.CODE)
                   .append(", attributes_properties.Type ").append(" AS ").append(AttributesPropertiesDBCols.TYPE)
                   .append(", attributes_properties.Descr ").append(" AS ").append(AttributesPropertiesDBCols.DESCR)
                   .append(", attributes_properties.attributes_properties_category_ID ").append(" AS ").append(AttributesPropertiesDBCols.CATEGORY_ID)
                   .append(", attributes_properties_category.Name ").append(" AS ").append(AttributesPropertiesDBCols.CATEGORY_NAME)
                   .append(" FROM ")
                   .append(" attributes_properties ")
                   .append("INNER JOIN main_project ON (attributes_properties.MainPrjID = main_project.ID)")
                   .append("INNER JOIN attributes_properties_category ON (attributes_properties.attributes_properties_category_ID = attributes_properties_category.ID)");

            stmnt = con.prepareStatement(sSelectSQL + sWhereClause + sOrderBy);




            if (attributesPropertiesSrchCriterionDTO != null && attributesPropertiesSrchCriterionDTO.isFilter()) {
                int colCount = 1;


                if (!util.isEmpty(attributesPropertiesSrchCriterionDTO.getID())) {
                    stmnt.setInt(colCount++, attributesPropertiesSrchCriterionDTO.getID());
                }

                if (!util.isEmpty(attributesPropertiesSrchCriterionDTO.getMainPrjID())) {
                    stmnt.setInt(colCount++, attributesPropertiesSrchCriterionDTO.getMainPrjID());
                }

                if (!util.isEmpty(attributesPropertiesSrchCriterionDTO.getCode())) {
                    stmnt.setString(colCount++, attributesPropertiesSrchCriterionDTO.getCode());
                }

                if (!util.isEmpty(attributesPropertiesSrchCriterionDTO.getType())) {
                    stmnt.setString(colCount++, attributesPropertiesSrchCriterionDTO.getType());
                }

                if (!util.isEmpty(attributesPropertiesSrchCriterionDTO.getDescr())) {
                    stmnt.setString(colCount++, attributesPropertiesSrchCriterionDTO.getDescr());
                }
                if (!util.isEmpty(attributesPropertiesSrchCriterionDTO.getCategoryID())) {
                    stmnt.setInt(colCount++, attributesPropertiesSrchCriterionDTO.getCategoryID());
                }

            }

            rs = stmnt.executeQuery();
            AttributesPropertiesTable attributesPropertiesTable = null;

            while (rs.next()) {
                attributesPropertiesTable = new AttributesPropertiesTable();



                attributesPropertiesTable.setID(rs.getInt(AttributesPropertiesDBCols.ID));


                attributesPropertiesTable.setMainPrjID(rs.getInt(AttributesPropertiesDBCols.MAINPRJID));


                attributesPropertiesTable.setMainPrjName(util.removeNullFrom(rs.getString(AttributesPropertiesDBCols.MAINPRJNAME)));
                
                attributesPropertiesTable.setCode(util.removeNullFrom(rs.getString(AttributesPropertiesDBCols.CODE)));


                attributesPropertiesTable.setType(util.removeNullFrom(rs.getString(AttributesPropertiesDBCols.TYPE)));


                attributesPropertiesTable.setDescr(util.removeNullFrom(rs.getString(AttributesPropertiesDBCols.DESCR)));
                
                attributesPropertiesTable.setCategoryID(rs.getInt(AttributesPropertiesDBCols.CATEGORY_ID));
                
                attributesPropertiesTable.setCategoryName(util.removeNullFrom(rs.getString(AttributesPropertiesDBCols.CATEGORY_NAME)));


                attributesPropertiesTables.add(attributesPropertiesTable);
                attributesPropertiesTable = null;
            }

        } catch (SQLException sqlEx) {
            logger.log(Level.WARNING, "SQLException in AttributesPropertiesDAO.insert method Message: {0}", sqlEx.getMessage());
            throw new Exception(
                    "SQLException in method:AttributesPropertiesDAO.getByCriterionOrderedBy"
                    + sqlEx.getMessage());
        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception in AttributesPropertiesDAO.insert method Message: {0}", e.getMessage());
            throw new Exception(
                    "Exception in method:AttributesPropertiesDAO.getByCriterionOrderedBy"
                    + e.getMessage());
        } finally {
            if (!isConnSupplied) {
                ResourceManager.close(con);
            }
        }
        return attributesPropertiesTables;
    }

    private String getRetrieveWhereClause(AttributesPropertiesTable attributesPropertiesSrchCriterionDTO)
            throws Exception {


        StringBuffer sbWhereClause = new StringBuffer();
        String sAnd = "";
        boolean bPrevColExistsInWhereClause = false;
        boolean bAtLeastOneFieldExists = false;

        if (!util.isEmpty(attributesPropertiesSrchCriterionDTO.getID())) {
            bAtLeastOneFieldExists = true;
            sbWhereClause.append(" ID =").append(" ? ");
            bPrevColExistsInWhereClause = true;
        }

        if (attributesPropertiesSrchCriterionDTO.Opr != null) {



            if (!util.isEmpty(attributesPropertiesSrchCriterionDTO.Opr.getIDOpr().trim()) && !util.isEmpty(attributesPropertiesSrchCriterionDTO.getID())) {
                bAtLeastOneFieldExists = true;
                if (bPrevColExistsInWhereClause) {
                    sAnd = " AND ";
                } else {
                    sAnd = "";
                }
                sbWhereClause.append(sAnd).append(" " + AttributesPropertiesDBCols.ID + "  ").append(attributesPropertiesSrchCriterionDTO.Opr.getIDOpr().trim()).append("?");
                bPrevColExistsInWhereClause = true;
            }
            
            if (!util.isEmpty(attributesPropertiesSrchCriterionDTO.Opr.getCategoryIDOpr().trim()) && !util.isEmpty(attributesPropertiesSrchCriterionDTO.getCategoryID())) {
                bAtLeastOneFieldExists = true;
                if (bPrevColExistsInWhereClause) {
                    sAnd = " AND ";
                } else {
                    sAnd = "";
                }
                sbWhereClause.append(sAnd).append(" " + AttributesPropertiesDBCols.CATEGORY_ID + "  ").append(attributesPropertiesSrchCriterionDTO.Opr.getCategoryIDOpr().trim()).append("?");
                bPrevColExistsInWhereClause = true;
            }


            if (!util.isEmpty(attributesPropertiesSrchCriterionDTO.Opr.getMainPrjIDOpr().trim()) && !util.isEmpty(attributesPropertiesSrchCriterionDTO.getMainPrjID())) {
                bAtLeastOneFieldExists = true;
                if (bPrevColExistsInWhereClause) {
                    sAnd = " AND ";
                } else {
                    sAnd = "";
                }
                sbWhereClause.append(sAnd).append(" " + AttributesPropertiesDBCols.MAINPRJID + "  ").append(attributesPropertiesSrchCriterionDTO.Opr.getMainPrjIDOpr().trim()).append("?");
                bPrevColExistsInWhereClause = true;
            }


            if (!util.isEmpty(attributesPropertiesSrchCriterionDTO.Opr.getCodeOpr().trim()) && !util.isEmpty(attributesPropertiesSrchCriterionDTO.getCode().trim())) {
                bAtLeastOneFieldExists = true;
                if (bPrevColExistsInWhereClause) {
                    sAnd = " AND ";
                } else {
                    sAnd = "";
                }
                if (attributesPropertiesSrchCriterionDTO.Opr.getCodeOpr().trim().equalsIgnoreCase("LIKE")) {
                    sbWhereClause.append(sAnd).append(" " + AttributesPropertiesDBCols.CODE + " LIKE").append(" ? ");
                } else {
                    sbWhereClause.append(sAnd).append(" " + AttributesPropertiesDBCols.CODE + "  ").append(attributesPropertiesSrchCriterionDTO.Opr.getCodeOpr().trim()).append("?");
                }
                bPrevColExistsInWhereClause = true;
            }


            if (!util.isEmpty(attributesPropertiesSrchCriterionDTO.Opr.getTypeOpr().trim()) && !util.isEmpty(attributesPropertiesSrchCriterionDTO.getType().trim())) {
                bAtLeastOneFieldExists = true;
                if (bPrevColExistsInWhereClause) {
                    sAnd = " AND ";
                } else {
                    sAnd = "";
                }
                if (attributesPropertiesSrchCriterionDTO.Opr.getTypeOpr().trim().equalsIgnoreCase("LIKE")) {
                    sbWhereClause.append(sAnd).append(" " + AttributesPropertiesDBCols.TYPE + " LIKE").append(" ? ");
                } else {
                    sbWhereClause.append(sAnd).append(" " + AttributesPropertiesDBCols.TYPE + "  ").append(attributesPropertiesSrchCriterionDTO.Opr.getTypeOpr().trim()).append("?");
                }
                bPrevColExistsInWhereClause = true;
            }


            if (!util.isEmpty(attributesPropertiesSrchCriterionDTO.Opr.getDescrOpr().trim()) && !util.isEmpty(attributesPropertiesSrchCriterionDTO.getDescr().trim())) {
                bAtLeastOneFieldExists = true;
                if (bPrevColExistsInWhereClause) {
                    sAnd = " AND ";
                } else {
                    sAnd = "";
                }
                if (attributesPropertiesSrchCriterionDTO.Opr.getDescrOpr().trim().equalsIgnoreCase("LIKE")) {
                    sbWhereClause.append(sAnd).append(" " + AttributesPropertiesDBCols.DESCR + " LIKE").append(" ? ");
                } else {
                    sbWhereClause.append(sAnd).append(" " + AttributesPropertiesDBCols.DESCR + "  ").append(attributesPropertiesSrchCriterionDTO.Opr.getDescrOpr().trim()).append("?");
                }
                bPrevColExistsInWhereClause = true;
            }




        }

        if (sbWhereClause.toString().trim().length() <= 0) {
            logger.log(Level.WARNING, "getRetrieveWhereClause Cannot return Null  ");
            throw new Exception("getRetrieveWhereClause Cannot return Null  ");
        } else {
            return sbWhereClause.toString().trim();
        }
    }

    private String getOrderBy(ColToOrderBy colToOrderBy) {
        String sOrderBy = " Order By ";

        sOrderBy = sOrderBy + colToOrderBy.Name;

        if (colToOrderBy.OrderType != null) {
            sOrderBy = sOrderBy + " " + colToOrderBy.OrderType;
        }
        return sOrderBy;
    }

    public int deleteByCriterion(IBaseTable recordsToDeleteCritPar)
            throws Exception {

        final boolean isConnSupplied = (attributesPropertiesConn != null);
        AttributesPropertiesTable recordsToDeleteCrit = (AttributesPropertiesTable) recordsToDeleteCritPar;
        int iNoOfRecordsDeleted = -1;
        Connection con = null;
        PreparedStatement stmnt = null;

        StringBuffer sbDeleteSQLStmnt = new StringBuffer();
        sbDeleteSQLStmnt.append("DELETE from ").append(AttributesPropertiesDAO.TblName).append(" WHERE ");

        String sWhereClause = "";
        if (recordsToDeleteCrit != null && recordsToDeleteCrit.isFilter()) {
            try {
                sWhereClause = this.getRetrieveWhereClause(recordsToDeleteCrit);
            } catch (Exception e) {
                System.out.println("getByCriterionOrderedBy.Exception..");
                throw new Exception(" AttributesProperties.deleteByCriterion.Exception:  "
                        + e.getMessage());
            }

            if (sWhereClause != null
                    && !sWhereClause.trim().equalsIgnoreCase("")) {
                sWhereClause = " WHERE " + sWhereClause;
            }

            sbDeleteSQLStmnt.append(sWhereClause);
        }
        try {
            con = isConnSupplied ? attributesPropertiesConn : ResourceManager.getConnection();
            stmnt = con.prepareStatement(sbDeleteSQLStmnt.toString());

            if (recordsToDeleteCrit != null && recordsToDeleteCrit.isFilter()) {
            }
            //  iNoOfRecordsDeleted = stmnt.executeUpdate ();



            return iNoOfRecordsDeleted;
        } catch (SQLException sqlEx) {
            logger.log(Level.WARNING, "SQLException in AttributesPropertiesDAO.deleteByCriterion method Message: {0}", sqlEx.getMessage());
            throw new Exception(
                    "SQLException in AttributesPropertiesDAO.deleteByCriterion method");
        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception in AttributesPropertiesDAO.deleteByCriterion method Message: {0}", e.getMessage());
            throw new Exception(
                    "Exception in AttributesPropertiesDAO.deleteByCriterion method");
        } finally {

            if (!isConnSupplied) {
                ResourceManager.close(con);
            }
        }
    }
}
